package com.qingke.easyjava.jdbc.command.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.qingke.easyjava.jdbc.app.BeSuperApplication;
import com.qingke.easyjava.jdbc.app.BeSuperDao;
import com.qingke.easyjava.jdbc.command.PlayerCommand;
import com.qingke.easyjava.jdbc.pojo.Answer;
import com.qingke.easyjava.jdbc.pojo.Player;
import com.qingke.easyjava.jdbc.pojo.Question;
import com.qingke.easyjava.jdbc.util.QingkeConsole;

public class ListCommand extends PlayerCommand {

    public ListCommand(Player player) {
        super(player);
    }

    @Override
    public void executePlayerCommand() {
        String listMode = QingkeConsole.askUserInput("选择你要列出的问题 (ALL/MINE/OPEN/ID)");//所有，我的问题，被解决问题，问题id
        BeSuperDao dao = new BeSuperDao();
        System.out.println(player);
        if (!"ALL|MINE|OPEN|ID".contains(listMode.toUpperCase())) {
            QingkeConsole.println("请使用支持的模式！");
            return;
        }

        List<Question> questions = new ArrayList<>();
        if ("ALL".equalsIgnoreCase(listMode)) {
            // do all
            questions = dao.getQuestions();
        } else if ("MINE".equalsIgnoreCase(listMode)) {
            // do mine
            questions = dao.getQuestionsFrom(player);
        } else if ("OPEN".equalsIgnoreCase(listMode)) {
            // do open
            List<Question> tmpList = dao.getQuestions();
            
            for (Question question : tmpList) {
                if (question.isOpen()) {
                    questions.add(question);
                }
            }
            
        } else if ("ID".equalsIgnoreCase(listMode)) {
            long id = QingkeConsole.askUserInputInt("请输入问题ID");
            
            Question q = dao.getQuestion(id);
            questions.add(q);
        }
        
        for (Question q : questions) {
            QingkeConsole.println(q);
        }
    }
    
    /*    public List<Question> getQuestions() {
        List<Question> questions = new ArrayList<Question>();
        Connection conn = null;
        PreparedStatement statement = null;
        ResultSet rs = null;

        String sql = "SELECT id, value, credit, player_id, isOpen FROM question order by isOpen";
        try {
            conn = getConnection();
            statement = conn.prepareStatement(sql);

            rs = statement.executeQuery();
            while (rs.next()) {
                long id = rs.getLong("id");
                String value =rs.getString("value");
                int credit = rs.getInt("credit");
                long playerId = rs.getLong("player_id");
                boolean isOpen = "Y".equals(rs.getString("isOpen"));
                
                Player player = this.getPlayer(playerId);
                
                Question question = new Question(id, value, player, credit);
                question.setOpen(isOpen);

                List<Answer> answers = this.getAnswersFrom(question);
                question.setAnswers(answers);

                questions.add(question);
            }

            return questions;
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            cleanup(conn, statement, rs);
        }
        return questions;
    }*/
    
    /*    public Question getQuestion(long questionId) {
        Question question = null;
        Connection conn = null;
        PreparedStatement statement = null;
        ResultSet rs = null;

        String sql = "SELECT id, value, player_id, isOpen, credit FROM question where id = ?";
        try {
            conn = getConnection();
            statement = conn.prepareStatement(sql);
            
            statement.setLong(1, questionId);
            
            rs = statement.executeQuery();
            
            while(rs.next()) {
                long id = rs.getInt("id");
                String value = rs.getString("value");
                long playerId = rs.getLong("player_id");
                int credit = rs.getInt("credit");
                boolean isOpen = "Y".equals(rs.getString("isOpen"));
                
                Player player = this.getPlayer(playerId);
                question = new Question(id, value, player, credit);
                question.setOpen(isOpen);

                List<Answer> answers = this.getAnswersFrom(question);
                question.setAnswers(answers);
            }

            return question;
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            cleanup(conn, statement, rs);
        }
        return question;
    }*/
    
}
